package com.zsrt.java.day20161228.wrl.demo02;

import java.util.Arrays;

public class PopSort02 {
	
	public static void main(String[] args) {
		
		int[] arrays = {50,40,30,80,20};
		System.out.println(Arrays.toString(arrays));
		
		System.out.println("冒泡排序...");
		arrays = popSortTest(arrays);
		System.out.println(Arrays.toString(arrays));
	}
	
	/**
	 * 冒泡排序:相邻两个比较，大的放在后面
	 * 
	 * {50,40,30,80,20};
	 * 				                        第几轮          循环次数			a	      跟           b  比较
	 * 		length=5 		outer  length-outer	   index-1	index
	 * 01 12 23 04			 1		4				
	 * 01 12 23			 	 2		3
	 * 01 12				 3		2
	 * 01					 4		1
	 * 
	 * @param arrays 需要排序的数组
	 */
	public static int[] popSortTest(int[] arrays){
		int length   = arrays.length;
		for(int outer = 0; outer < length; outer++){
			for(int inner = 1; inner < length-outer; inner++){
				
				//比较，相邻两个比较
				int index1 = inner-1;
				int index2 = inner;
				if(arrays[index1] > arrays[index2]){
					int  temp      = arrays[index1];
					arrays[index1] = arrays[index2];
					arrays[index2] = temp;
				}
			}
		}
		return arrays;
	}
}
